package com.baitengsoft.patentapimonitor.api.controller;

import com.baitengsoft.patentapimonitor.api.entity.WebMenuEntity;
import com.baitengsoft.patentapimonitor.api.entity.WebPlatformEntity;
import com.baitengsoft.patentapimonitor.api.model.DataRangeModel;
import com.baitengsoft.patentapimonitor.api.model.WebMenuModel;
import com.baitengsoft.patentapimonitor.api.model.WebUserInfoModel;
import com.baitengsoft.patentapimonitor.api.model.WebUserInfoViewModel;
import com.baitengsoft.patentapimonitor.api.model.param.EditUserInfoParam;
import com.baitengsoft.patentapimonitor.api.model.param.UserInfoParam;
import com.baitengsoft.patentapimonitor.api.service.WebPlatformService;
import com.baitengsoft.patentapimonitor.api.service.WebUserService;
import com.baitengsoft.patentapimonitor.common.utils.PageResult;
import com.baitengsoft.patentapimonitor.common.utils.Res;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Admin on 2017/12/26.
 */

@RestController
@RequestMapping("api/v1/admin")
@Api(value = "后台-用户管理",description = "用户管理")
public class AdminUserInfoController extends AbstractController{
    @Autowired
    WebUserService webUserService;
    @Autowired
    WebPlatformService webPlatformService;

    @ApiOperation(value = "用户管理列表")
    @RequestMapping(value="/searchuserinfos", method= RequestMethod.POST)
    public Res searchUserInfos(@RequestBody UserInfoParam param){

        Map<String,Object> map=new HashMap<>();
        Boolean flag=false;
        try {
            PageResult<WebUserInfoModel> pageResult= webUserService.searchUserInfos(param);
            if(pageResult!=null){
                flag=true;
                map.put("data",pageResult);
            }else{
                map.put("msg","暂无数据");
            }
        } catch (Exception e) {
            map.put("msg","获取数据异常");
        }
        map.put("flag",flag);
        return Res.ok(map);
    }


    /**
     * 新增与编辑
     * @param param
     * @return
     */
    @ApiOperation(value = "编辑用户")
    @RequestMapping(value="/edituserinfos", method= RequestMethod.POST)
    public Res editUserInfos(@RequestBody EditUserInfoParam param){

        Map<String,Object> map=new HashMap<>();
        Boolean flag=false;
        try {
            Integer successId = webUserService.editUserInfo(param,getAdminUserId());
            if(successId==1){
                flag=true;
                map.put("msg","操作成功");
            }else{
                map.put("msg","操作失败");
            }
        } catch (Exception e) {
            map.put("msg","操作异常");
        }
        map.put("flag",flag);
        return Res.ok(map);
    }


    @ApiOperation(value = "获取平台列表")
    @RequestMapping(value="/searchplatformsforuser", method= RequestMethod.GET)
    public Res searchPlatformsForUser(){

        Map<String,Object> map=new HashMap<>();
        Boolean flag=false;
        try {
            List<WebPlatformEntity> entities= webPlatformService.searchPlatformsForUser();
            if(entities!=null){
                flag=true;
                map.put("data",entities);
            }else{
                map.put("msg","暂无数据");
            }
        } catch (Exception e) {
            map.put("msg","操作异常");
        }
        map.put("flag",flag);
        return Res.ok(map);
    }

    @ApiOperation(value = "获取平台菜单")
    @RequestMapping(value="/searchmenuforuser", method= RequestMethod.GET)
    public Res searchMenuForUser(@RequestParam("platformId") int platformId){

        Map<String,Object> map=new HashMap<>();
        Boolean flag=false;
        try {
            List<WebMenuModel> models= webUserService.searchMenusByPlatformId(platformId);
            if(models!=null){
                flag=true;
                map.put("data",models);
            }else{
                map.put("msg","暂无数据");
            }
        } catch (Exception e) {
            map.put("msg","操作异常");
        }
        map.put("flag",flag);
        return Res.ok(map);
    }


    @ApiOperation(value = "获取平台数据范围")
    @RequestMapping(value="/searchdatarangeforuser", method= RequestMethod.GET)
    public Res searchDataRangeForUser(@RequestParam("platformId") int platformId){

        Map<String,Object> map=new HashMap<>();
        Boolean flag=false;
        try {
            DataRangeModel model= webUserService.searchDrByPlatformId(platformId);
            if(model!=null){
                flag=true;
                map.put("data",model);
            }else{
                map.put("msg","暂无数据");
            }
        } catch (Exception e) {
            map.put("msg","操作异常");
        }
        map.put("flag",flag);
        return Res.ok(map);
    }


    @ApiOperation(value = "获取用户信息")
    @RequestMapping(value="/getuserinfodetail", method= RequestMethod.GET)
    public Res getUserInfoDetail(@RequestParam("id") int id){

        Map<String,Object> map=new HashMap<>();
        Boolean flag=false;
        try {
            WebUserInfoViewModel model= webUserService.searchUserInfoById(id);
            if(model!=null){
                flag=true;
                map.put("data",model);
            }else{
                map.put("msg","暂无数据");
            }
        } catch (Exception e) {
            map.put("msg","操作异常");
        }
        map.put("flag",flag);
        return Res.ok(map);
    }


    @ApiOperation(value = "验证用户姓名")
    @RequestMapping(value="/checkusername", method= RequestMethod.GET)
    public Res checkUserName(@RequestParam("userName") String userName){

        Map<String,Object> map=new HashMap<>();
        Boolean flag=false;
        try {
            Boolean result= webUserService.checkUserName(userName);
            if(result){
                flag=true;
                map.put("msg","验证通过");
            }else{
                map.put("msg","用户名已存在");
            }
        } catch (Exception e) {
            map.put("msg","验证异常");
        }
        map.put("flag",flag);
        return Res.ok(map);
    }

    @ApiOperation(value = "删除用户")
    @RequestMapping(value="/deluserinfo", method= RequestMethod.GET)
    public Res delUserinfo(@RequestParam("ids") String ids){

        Map<String,Object> map=new HashMap<>();
        Boolean flag=false;
        try {
            Integer result= webUserService.delUserInfoByIds(ids,getAdminUserId());
            if(result==1){
                flag=true;
                map.put("msg","删除成功");
            }else{
                map.put("msg","删除失败");
            }
        } catch (Exception e) {
            map.put("msg","删除异常");
        }
        map.put("flag",flag);
        return Res.ok(map);
    }


    /**
     * 启用  禁止
     * @param id
     * @param status
     * @return
     */
    @ApiOperation(value = "修改用户状态")
    @RequestMapping(value="/setstatusofuserinfo", method= RequestMethod.GET)
    public Res setStatusOfUserinfo(@RequestParam("id") int id,@RequestParam("status") int status){

        Map<String,Object> map=new HashMap<>();
        Boolean flag=false;
        try {
            Integer result= webUserService.setStatusOfUserInfo(id,status,getAdminUserId());
            if(result==1){
                flag=true;
                map.put("msg","修改成功");
            }else{
                map.put("msg","修改失败");
            }
        } catch (Exception e) {
            map.put("msg","修改异常");
        }
        map.put("flag",flag);
        return Res.ok(map);
    }

}
